package com.shop.cereshop.agent.controller.behavior;

import com.shop.cereshop.agent.page.collect.AgentCollectListVO;
import com.shop.cereshop.agent.page.user.CereAgentUserVO;
import com.shop.cereshop.agent.param.behavior.UpdateCollectDTO;
import com.shop.cereshop.agent.service.behavior.AgentBehaviorService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.agent.CereAgentRole;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.EmptyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 用户行为控制器
 */
@RestController
@RequestMapping("/behavior")
@Api(value = "用户行为管理", tags = "用户行为管理")
public class AgentBehaviorController {

  @Autowired
  private AgentBehaviorService agentBehaviorService;

  @PostMapping("collect/update")
  @ApiOperation(value = "更新用户收藏")
  public Result updateCollect(@RequestBody @Valid UpdateCollectDTO param, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    agentBehaviorService.updateCollect(param, user);
    return Result.success();
  }

  @GetMapping("collect/list")
  @ApiOperation(value = "获取用户收藏列表")
  public Result<List<AgentCollectListVO>> getCollectList(
      @RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "每页数量", required = true) Integer pageSize,
      HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(agentBehaviorService.getCollectList(page, pageSize, user));
  }

  @GetMapping("view/list")
  @ApiOperation(value = "获取用户浏览列表(足迹列表)")
  public Result<List<AgentCollectListVO>> getViewList(
      @RequestParam("page") @ApiParam(value = "页码", required = true) Integer page,
      @RequestParam("pageSize") @ApiParam(value = "每页数量", required = true) Integer pageSize,
      HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }
    return Result.success(agentBehaviorService.getViewList(page, pageSize, user));
  }

  @PostMapping("deletes")
  @ApiOperation(value = "批量删除用户行为")
  public Result removeBehaviorByIds(@RequestParam("ids") @ApiParam(value = "批量删除的id集合", required = true) List<Long> ids, HttpServletRequest request) {
    CereAgentUserVO user = (CereAgentUserVO) request.getAttribute("user");
    if (user == null) {
      return Result.unauthorized();
    }

    // 参数校验
    if (EmptyUtils.isEmpty(ids)) {
      return Result.failed(CoReturnFormat.PARAM_INVALID);
    }

    // 调用服务层方法
    boolean result = agentBehaviorService.removeBehaviorByIds(ids);
    return result ? Result.success() : Result.failed(CoReturnFormat.FAIL);
  }
}